﻿//////////////////////////////////////////////////////////////////////
/**
* 全局变量定义区
*/

// 是否输出日志到错误控制台
var debugMode = true;

//////////////////////////////////////////////////////////////////////
/**
* Flash交互操作类  CTP_Controller
*/

function playerController() {
	this.Url = "";
	this.Author = null;
	this.Title = null;
	this.Album = null;
	this.isPlay = false;
	this.loadComplete = false;
	this.Position = 0;
	this.Volume = 0;
	this.isLoad = false;
	this.playPercent = 0;
	this.curIndex = null;
};

playerController.prototype = {

	// 取得播放器句柄
	getPlayer: function() {

		var flashH = document.getElementById('ctp_player');

		return flashH;
	},

	// 加载文件
	loadFile: function() {
		this.getPlayer().cLoad(this.Url);

		myDump("loadFile: " + this.Url);

		setCTPText("正在连接...", null, null, null);

		this.isLoad = true;
	},

	// 播放暂停
	playPause: function() {

		if (this.isPlay == true) {
			//正在播放
			this.getPlayer().cStop();
			this.Position = this.getPosition();

			myDump("cStop: " + this.Position);

			setPlayPauseImage("play");

			this.isPlay = false;
			isScallMove = true;

			return;
		}

		//已经暂停
		if (this.isLoad == true) var Pos = this.Position;
		else var Pos = 0;

		if (this.isLoad == false) this.loadFile();
		this.getPlayer().cPlay(Pos);

		myDump("cPlay: " + Pos);

		setPlayPauseImage("pause");

		this.isPlay = true;
		isScallMove = false;
	},

	// 停止播放
	closeFile: function() {
		this.isLoad = false;
		this.isPlay = false;
		isScallMove = true;

		CTP_Controller.loadComplete = false;

		this.Author = null;
		this.Title = null;
		this.Album = null;

		setCTPText("准备就绪...", null, null, null);
		setPlayPauseImage("play");

		//第一次运行
		if (isFirstRun) {
			//设置检测播放能否工作定时器
			isFlashEnable = false;
			myDump("Flash Work Status Test Begin.");
			setTimeout("CTP_CheckIsFlashEnable()", 3000);

			isFirstRun = false;
		}

		this.getPlayer().cClose();

		myDump("closeFile: ");

		//能运行到这里说明Flash播放器正常工作，把变量设回来
		isFlashEnable = true;

		myDump("isFlashEnable Set To True");
		//--------------
	},

	// 取得上一次播放位置
	getPosition: function() {
		return this.getPlayer().getLastPosition();
	},

	// 取得文件长度
	getLength: function() {
		return this.getPlayer().getTotalLength();
	},

	//控制音量, 0 - 1
	setVolume: function(value) {
		this.getPlayer().setVolume(value);

	},

	// 取得当前的音量设置
	getVolume: function() {
		return this.getPlayer().getVolume();

	}
};

//------------------------------------------
var CTP_Controller = new playerController();

//------------------------------------------

//--------------------------
var CTP_Prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("extensions.clicktoplay.");

var CTP_browser = document.createElement("browser");

//--------------------------
var isLastPlaying = false;

var isWinShow = false;

var isScaleDrag = false;

var isScallMove = true;

var isUFInit = false;

var isUFUnInit = false;

var isCTPTextCanSet = true;

var isCTP_RealUrlPageLoadedCalled = false;

var isFlashEnable = false;

var isFirstRun = true;

var isCheckDialogShowed = false;

//////////////////////////////////////////////////////////////////////
/**
* 页面导航控制类  URLfetcher
*/
var URLfetcher = {

	PopupH: null,

	urlH: null,

	urlStr: null,

	isOpen: false,

	isLoad: false,

	// 初始化
	Init: function() {
		// add dom listeners
		//防止多次调用
		if (isUFUnInit) return;
		isUFUnInit = true;

		document.addEventListener("mouseover", URLfetcher.urlMouseOver, true);
		document.getElementById("content").addProgressListener(URLfetcher, Components.interfaces.nsIWebProgress.NOTIFY_STATE_DOCUMENT);

		// add html 
		var Popup = document.createElement("div");
		Popup.id = "CTP_icon";
		Popup.style.position = "absolute";
		Popup.style.width = "24px";
		Popup.style.height = "24px";
		Popup.style.backgroundImage = "url(chrome://clicktoplay/skin/icon.png)";
		Popup.style.display = "block";
		Popup.style.cursor = "pointer";

		URLfetcher.PopupH = Popup;

		URLfetcher.PopupH.addEventListener("click", URLfetcher.addPlay, false);

		// load settings
		CTP_Controller.Url = CTP_Prefs.getCharPref("lastUrl");
		
		CTP_addToURLList(CTP_Controller.Url);
		
		CTP_Controller.Position = CTP_Prefs.getIntPref("lastPosition");

		isLastPlaying = CTP_Prefs.getBoolPref("isPlay");
		
		var ProgramFirstRun = CTP_Prefs.getBoolPref("isFirstRun");
		
		if(ProgramFirstRun){
			loadURI("chrome://clicktoplay/content/help/Help.html");
			CTP_Prefs.setBoolPref("isFirstRun", false);
		}
		setTimeout("CTP_lastPlay()", 5000);
	},

	// 反初始化
	unInit: function() {
		//防止多次调用
		if (isUFInit) return;
		isUFInit = true;

		document.removeEventListener("mouseover", URLfetcher.urlMouseOver, true);
		document.getElementById("content").removeProgressListener(this);

		// save settings
		CTP_Prefs.setBoolPref("isPlay", CTP_Controller.isPlay);

		CTP_Stop();

		var Pos = CTP_Controller.playPercent * CTP_Controller.getLength();
		CTP_Prefs.setCharPref("lastUrl", CTP_Controller.Url);
		CTP_Prefs.setIntPref("lastPosition", Pos);
	},

	// 显示导般图标
	Show: function() {

		var Doc = URLfetcher.urlH.ownerDocument;
		Doc.body.appendChild(URLfetcher.PopupH);

		if (URLfetcher.isOpen) URLfetcher.Hide();

		var linkH = this.urlH;

		var scrollTop = window.content.scrollY;

		var left = GetAbsPoint(linkH).right + 3;
		var top = GetAbsPoint(linkH).top - 10 + scrollTop

		URLfetcher.PopupH.style.left = left + "px";
		URLfetcher.PopupH.style.top = top + "px";

		//show
		URLfetcher.PopupH.style.display = "block";
		URLfetcher.isOpen = true;
	},

	// 隐藏导航图标
	Hide: function() {
		URLfetcher.PopupH.display = "block";
		URLfetcher.isOpen = false;
	},

	/* mouse listeners */
	urlMouseOver: function(evt) {
		var tar = evt.target.getAttribute("href") ? evt.target: evt.target.parentNode;
		URLfetcher.checkIsUrl(tar);
	},

	// 检查是否为链接
	checkIsUrl: function(target) {
		// returns if we are not over an link
		var Url = target.getAttribute("href");
		if (Url == null) return;
		URLfetcher.urlH = target;
		if (CTP_CheckURL(Url) != -1) {
			// URL可用
			URLfetcher.urlStr = Url;
			myDump(URLfetcher.urlStr);
			// 显示导航图标
			URLfetcher.Show();
		}

	},

	//加入播放列表, 响应导航图标点击
	addPlay: function() {
		// 分析得到真正的URL
		CTP_getRealURL(URLfetcher.urlStr);
	}
};

//////////////////////////////////////////////////////////////////////
/**
* 播放列表控制类  CTP_URLList
*/

function CTP_URLListFunc() {
	this.curIndex = 0,
	this.curUrl = null

};

CTP_URLListFunc.prototype = {

};

//------------------------------------------
var CTP_URLList = new CTP_URLListFunc();

//------------------------------------------


//////////////////////////////////////////////////////////////////////
/**
* URL编码解码类  UrlH
*/

var UrlH = {

	// public method for url encoding
	encode: function(string) {
		return escape(this._utf8_encode(string));
	},

	// public method for url decoding
	decode: function(string) {
		return this._utf8_decode(unescape(string));
	},

	// private method for UTF-8 encoding
	_utf8_encode: function(string) {
		string = string.replace(/\r\n/g, "\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++) {

			var c = string.charCodeAt(n);

			if (c < 128) {
				utftext += String.fromCharCode(c);
			} else if ((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			} else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}

		}

		return utftext;
	},

	// private method for UTF-8 decoding
	_utf8_decode: function(utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while (i < utftext.length) {

			c = utftext.charCodeAt(i);

			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			} else if ((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i + 1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			} else {
				c2 = utftext.charCodeAt(i + 1);
				c3 = utftext.charCodeAt(i + 2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}

		}

		return string;
	}

}


//////////////////////////////////////////////////////////////////////
/**
* CPANEL 拖放事件监吸类 boardObserver
*/


var boardObserver = {
	
	getSupportedFlavours : function () { 

		var flavours = new FlavourSet(); 
	
		flavours.appendFlavour("text/unicode"); 
		
		flavours.appendFlavour("text/uri-list");
	
		return flavours; 

  	},
	
	onDragOver: function (event, flavour, session) {
			
	},

	onDrop: function (event, dropdata, session) {
		if (dropdata.data == "")	return;
		
		var url = dropdata.data;
		
		if(CTP_CheckURL(url) != -1){
			// 加入检测 	
			CTP_getRealURL(url);
			
		}else{
			alert("不支持的链接格式!");	
		}
	}
	
}